package com.jltx.xq.simplewatch.video.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;

import com.jltx.xq.simplewatch.entity.beans.Channel;
import com.jltx.xq.simplewatch.video.ui.VideoNewsListFragment;

import java.util.List;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/09/07.
 */

public class VideoFragmentPagerAdapter extends FragmentStatePagerAdapter{
    private Context context;
    private List<VideoTabFragmentInfo> fragmentInfoList;

    public VideoFragmentPagerAdapter(FragmentManager fm, Context context, List<VideoTabFragmentInfo> fragmentInfoList) {
        super(fm);
        if(context == null){
            throw new IllegalArgumentException("context is null");
        }
        this.context = context;
        this.fragmentInfoList = fragmentInfoList;
    }

    @Override
    public int getCount() {
        int count = 0;
        if(fragmentInfoList != null){
            count = fragmentInfoList.size();
        }
        return count;
    }

    @Override
    public Fragment getItem(int position) {
        if(fragmentInfoList != null){
            Channel channel = fragmentInfoList.get(position).channel;
            VideoNewsListFragment videoNewsListFragment = VideoNewsListFragment.newInstance(channel.title,channel.channel);
            return videoNewsListFragment;
        }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if(position >=0 && position < fragmentInfoList.size()){
            return fragmentInfoList.get(position).channel.title;
        }
        return "";
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    public static class VideoTabFragmentInfo{
        public Channel channel;

        public VideoTabFragmentInfo(Channel channel) {
            this.channel = channel;
        }
    }
}
